<?php
	if ( ! defined('BASEPATH')) exit('No direct script access allowed');  
	class Role extends MY_Controller{
		function __construct(){
			parent::__construct();
			$this->load->model('roles');
		}

		/**
		* 函数功能介绍：返回角色列表
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-22 10:41
		* 最后修改人：
		* 最后修改时间：
		*/
		function index($sort = 0, $asc_des = 0, $search = 0, $limit = 0){
			if($this->input->post()){
				$limit = 0;
				$post_data = $this->input->post('category');
				$search = $post_data['search'] ? urldecode($post_data['search']) : 0;
			}else{
				$search = urldecode($search);
			}
			$this->smarty->assign("action", '角色列表');
			$offset = 10;
			$current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			$conditions = array('sort' => $sort, 'asc_des' => $asc_des, 'search' => $search, 'limit' => $limit);
			$result = $this->roles->get_role_list($limit, $offset, $conditions);
			$this->load->library('pagination');
			$page_config['base_url'] = '/role/index/' . $sort . '/' . $asc_des . '/' . $search;
			$page_config['total_rows'] = $result['count'];
			$page_config['per_page'] = $offset; 
			$page_config['uri_segment'] = 6; 
			$this->pagination->initialize($page_config); 
			$pagination = $this->pagination->create_links();
			foreach ($result['result'] as $key => $value) {
				if($value['role_privilege'] == '') continue;
				$role_privilege = json_decode($value['role_privilege']);
				$result['result'][$key]['role_privilege'] = '';
				foreach ($role_privilege as $k => $val) {
					$result['result'][$key]['role_privilege'] .= get_menu_title_by_menu_url($val);
					if($k < count($role_privilege) - 1){
						$result['result'][$key]['role_privilege'] .= '、';
					}
				}
				if(strlen($result['result'][$key]['role_privilege']) > 200){
					$result['result'][$key]['role_privilege'] = mb_strcut($result['result'][$key]['role_privilege'], 0, 200, 'utf-8') . '...';
				}else{
					$result['result'][$key]['role_privilege'] = mb_strcut($result['result'][$key]['role_privilege'], 0, 200, 'utf-8');
				}
			}
			$this->smarty->assign("conditions", $conditions);
			$this->smarty->assign("current_url", $current_url);
			$this->smarty->assign("role_list", $result['result']);
			$this->smarty->assign("pagination", $pagination);
			$this->smarty->display("role/list.html");
		}

		/**
		* Add roles
		* create: 	Zhou Hailong
		* Created: 2014-08-22 9:34
		* 最后修改人：
		* Last Update：
		*/
		function add(){
			$privilege_list = $this->privileges->get_list();
			$this->smarty->assign("privilege_list", $privilege_list);
			$this->smarty->assign("action", "添加角色");
			$this->smarty->display('role/save.html');
		}

		/**
		* Save roles data
		* Create: 	Zhou Hailong
		* Cretaed: 2014-08-22 10:04
		* 最后修改人：
		* Last update：
		*/
		function save(){
			if( ! $this->input->post()) {
				show_message('/role', "非法操作！");exit;
			}
			$data = $this->input->post();
			$data['role_privilege'] = isset($data['role_privilege']) ? $data['role_privilege'] : array();
			$data['role_privilege'] = json_encode($data['role_privilege']);
			if($data['id'] > 0){
				$this->update_role($data);
			}else{
				$this->add_role($data);
			}
		}

		/**
		* 添加角色数据
		* Create: 	Zhou Hailong
		* Cretaed: 2014-08-22 10:14
		* 最后修改人：
		* Last update：
		*/
		function add_role($data){
			$role_info = $this->roles->get_field_by_where("id", "role_name = '{$data['role_name']}'");
			if(isset($role_info['id']) && $role_info['id'] > 0){
				show_message('-1', "角色名已存在");exit;
			}
			$data['create_on'] = date("Y-m-d H:i:s", time());
			$result = $this->roles->insert($data);
			if($result == TRUE){
				show_message('/role', "角色添加成功");
			}else{
				show_message('/role', "添加失败，请重试！");
			}
		}

		/**
		* 函数功能介绍：查看角色详情
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-22 11:12
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		*/
		function view($id){
			$role_info = $this->roles->get_role_info($id);
			$role_info['role_privilege'] = json_decode($role_info['role_privilege']);
			$this->smarty->assign("action", '查看角色');
			$this->smarty->assign("role_info", $role_info);
			$this->smarty->display("role/view.html");
		}

		/**
		* 函数功能介绍：编辑角色
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-22 14:11
		* 最后修改人：
		* 最后修改时间：
		* @param id int
		*/
		function edit($id){
			$privilege_list = $this->privileges->get_list();
			$this->smarty->assign("privilege_list", $privilege_list);
			$role_info = $this->roles->get_role_info($id);
			$role_info['role_privilege'] = json_decode($role_info['role_privilege']);
			$this->smarty->assign("action", '编辑角色');
			$this->smarty->assign("role_info", $role_info);
			$this->smarty->display("role/save.html");
		}

		/**
		* 修改角色数据
		* Create: 	Zhou Hailong
		* Cretaed: 2014-08-22 15:09
		* 最后修改人：
		* Last update：
		*/
		function update_role($data){
			$this->db->where('id', $data['id']);
 			$this->db->update('sys_roles', $data);
 			show_message($_SERVER['HTTP_REFERER'], "角色信息修改成功");
		}

		/**
		* 函数功能介绍：删除权限菜单
		* 创建人: 	Zhou Hailong
		* 创建时间: 2014-08-21 10:04
		* 最后修改人：
		* 最后修改时间：
		* @param ids int
		*/
		function delete($id = 0){
			if($_POST){
				$ids = $this->input->post('ids');
				if(count($ids) > 0){
					$this->roles->batch_delete($ids);
					show_message($_SERVER['HTTP_REFERER'], "角色信息删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "您没有选中任何数据");exit;
				}
			}elseif( (int) $id > 0){
				$result = $this->roles->delete($id);
				if($this->db->affected_rows() > 0){
					show_message($_SERVER['HTTP_REFERER'], "角色信息删除成功");
				}else{
					show_message($_SERVER['HTTP_REFERER'], "角色信息删除失败，请重试！");
				}
			}
		}
	}